clear 
***********
* Table 3 *
***********
* non-ISIC version
/* 1. get the regsave files together 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
global clist "FE FD FDFE"
*
foreach c of global clist {
qui use temp_leadlag_tab3_`c',clear 
qui keep if strpos(var,"ltradeshock")>0
qui replace var = "ltradeshock_l0" if var == "ltradeshock3" | var =="ltradeshock6"
qui drop N r2
qui gen period = substr(var,-2,2)
qui replace period = subinstr(period,"l","+",1)
qui replace period = subinstr(period,"f","-",1)
qui destring period, replace
qui drop var 
qui rename coef beta_`c'
qui rename stderr se_`c'
qui label var beta_`c' "Coef. `c' "
qui gen upper_`c' = beta_`c' + 1.96*se_`c'
qui gen down_`c'  = beta_`c' - 1.96*se_`c'

if "`c'" == "FE" {
qui save temp_leadlag_tab3_graph,replace
}
else {
qui merge 1:1 period using temp_leadlag_tab3_graph
qui drop _merge 
qui save temp_leadlag_tab3_graph,replace
}
di "Done with regression:  " "`c'" 
}


/* 2. size of increment between coefficients 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
global s : list sizeof global(clist)
global inc = (.3)/($s-1)


/* 3. For graph with 4 series set the colors 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
local col1 gs7*0.8 
local col2 blue*1
local col3 red*0.8 
local col4 red*1

/* 4. Loop
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
local t = 0
global gphbody
cap drop period_*

foreach c of global clist {
	local t = `++t'
	dis `t'
	gen period_`t' = period -.15 + ${inc}*(`t'-1)
	
	global gphbody $gphbody (connected beta_`c' period_`t', lcolor(`col`t'') mcolor(`col`t'') lpattern(dash) ) ///
				 (rcap upper_`c' down_`c' period_`t', lcolor(`col`t'') mcolor(`col`t'')) ///

				 
	}
*
graph twoway $gphbody ///
	,xline(0, lcolor(black) lpattern(dash) lwidth(thin)) yline(0, lcolor(black) lwidth(thin)) ///
	graphregion(color(white) lwidth(vvthin)) ///
	xlabel(-2(1)2) ylabel(#10, grid glcolor(gs12) glwidth(vvthin) ) ///
    xtitle("# of years since shock") ///
	leg(on order(1 3 5 7 ) col(1) ring(0) pos(10) symxsize(5) )  ysize(3) xsize(3)
* 
graph export $DIRLOG/Replication/graph_tab3_leadlag_panela.pdf, as(pdf) replace
***	
* -ISIC version
/* 1. get the regsave files together 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
global clist "FE FD FDFE"
*
foreach c of global clist {
qui use temp_leadlag_tab3_`c'i,clear 
qui keep if strpos(var,"ltradeshock")>0
qui replace var = "ltradeshock_l0" if var == "ltradeshock3" | var =="ltradeshock6" | var == "ltradeshock3i" | var =="ltradeshock6i"
qui drop N r2
qui gen period = substr(var,-2,2)
qui replace period = subinstr(period,"l","+",1)
qui replace period = subinstr(period,"f","-",1)
qui destring period, replace
qui drop var 
qui rename coef beta_`c'
qui rename stderr se_`c'
qui label var beta_`c' "Coef. `c' "
qui gen upper_`c' = beta_`c' + 1.96*se_`c'
qui gen down_`c'  = beta_`c' - 1.96*se_`c'

if "`c'" == "FE" {
qui save temp_leadlag_tab3_graph,replace
}
else {
qui merge 1:1 period using temp_leadlag_tab3_graph
qui drop _merge 
qui save temp_leadlag_tab3_graph,replace
}
di "Done with regression:  " "`c'" 
}


/* 2. size of increment between coefficients 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
global s : list sizeof global(clist)
global inc = (.3)/($s-1)


/* 3. For graph with 4 series set the colors 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
local col1 gs7*0.8 
local col2 blue*1
local col3 red*0.8 
local col4 red*1

/* 4. Loop
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
local t = 0
global gphbody
cap drop period_*

foreach c of global clist {
	local t = `++t'
	dis `t'
	gen period_`t' = period -.15 + ${inc}*(`t'-1)
	
	global gphbody $gphbody (connected beta_`c' period_`t', lcolor(`col`t'') mcolor(`col`t'') lpattern(dash) ) ///
				 (rcap upper_`c' down_`c' period_`t', lcolor(`col`t'') mcolor(`col`t'')) ///

				 
	}
*
graph twoway $gphbody ///
	,xline(0, lcolor(black) lpattern(dash) lwidth(thin)) yline(0, lcolor(black) lwidth(thin)) ///
	graphregion(color(white) lwidth(vvthin)) ///
	xlabel(-2(1)2) ylabel(#10, grid glcolor(gs12) glwidth(vvthin) ) ///
    xtitle("# of years since shock") ///
	leg(on order(1 3 5 7 ) col(1) ring(0) pos(10) symxsize(5) )  ysize(3) xsize(3)
* 
graph export $DIRLOG/Replication/graph_tab3_leadlag_panelb.pdf, as(pdf) replace
*
*
erase temp_leadlag_tab3_graph.dta


***********
* Table 6 *
***********
* non-ISIC version
/* 1. get the regsave files together 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
global clist "FE FD FDFE"
*
foreach c of global clist {
qui use temp_leadlag_tab7_`c',clear 
qui keep if strpos(var,"shock3intens")>0 | strpos(var,"shock6intens")>0
qui replace var = "shock3intens_l0" if var == "shock3intens" 
qui replace var = "shock6intens_l0" if var == "shock6intens" 
qui drop N r2
qui gen period = substr(var,-2,2)
qui replace period = subinstr(period,"l","+",1)
qui replace period = subinstr(period,"f","-",1)
qui destring period, replace
qui drop var 
qui rename coef beta_`c'
qui rename stderr se_`c'
qui label var beta_`c' "Coef. `c' "
qui gen upper_`c' = beta_`c' + 1.96*se_`c'
qui gen down_`c'  = beta_`c' - 1.96*se_`c'

if "`c'" == "FE" {
qui save temp_leadlag_tab7_graph,replace
}
else {
qui merge 1:1 period using temp_leadlag_tab7_graph
qui drop _merge 
qui save temp_leadlag_tab7_graph,replace
}
di "Done with regression:  " "`c'" 
}


/* 2. size of increment between coefficients 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
global s : list sizeof global(clist)
global inc = (.3)/($s-1)


/* 3. For graph with 4 series set the colors 
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
local col1 gs7*0.8 
local col2 blue*1
local col3 red*0.8 
local col4 red*1

/* 4. Loop
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ */
local t = 0
global gphbody
cap drop period_*

foreach c of global clist {
	local t = `++t'
	dis `t'
	gen period_`t' = period -.15 + ${inc}*(`t'-1)
	
	global gphbody $gphbody (connected beta_`c' period_`t', lcolor(`col`t'') mcolor(`col`t'') lpattern(dash) ) ///
				 (rcap upper_`c' down_`c' period_`t', lcolor(`col`t'') mcolor(`col`t'')) ///

				 
	}
*
graph twoway $gphbody ///
	,xline(0, lcolor(black) lpattern(dash) lwidth(thin)) yline(0, lcolor(black) lwidth(thin)) ///
	graphregion(color(white) lwidth(vvthin)) ///
	xlabel(-2(1)2) ylabel(#10, grid glcolor(gs12) glwidth(vvthin) ) ///
    xtitle("# of years since shock") ///
	leg(on order(1 3 5 7 ) col(1) ring(0) pos(10) symxsize(5) )  
	*ysize(3) xsize(3)
* 
graph export $DIRLOG/Replication/graph_tab7_leadlag_panela.pdf, as(pdf) replace
*
erase temp_leadlag_tab7_graph.dta



*
erase temp_leadlag_tab3_FE.dta
erase temp_leadlag_tab3_FD.dta
erase temp_leadlag_tab3_FDFE.dta

erase temp_leadlag_tab7_FE.dta
erase temp_leadlag_tab7_FD.dta
erase temp_leadlag_tab7_FDFE.dta




